package ru.stepuk.modern.nodes;

import javax.swing.ImageIcon;
import javax.swing.JDialog;
import ru.stepuk.modern.InvalidModelException;
import ru.stepuk.modern.Model;
import ru.stepuk.modern.Request;
import ru.stepuk.modern.gui.PrioritySetterDialog;

/**
 *
 * @author ivan
 */
public class PrioritySetter extends Node {

    private int newPriority;
    private Node next;

    {
        icon = new ImageIcon(getClass().getResource("/images/o_prior.png"));
        name = "Priority_setter " + getId();
    }

    public PrioritySetter(Model model) {
        super(model);
    }

    public int getNewPriority() {
        return newPriority;
    }

    public void setNewPriority(int newPriority) {
        this.newPriority = newPriority;
    }

    @Override
    public void processRequest(Request request) {
        request.setPriority(newPriority);
        next.processRequest(request);
    }

    @Override
    public void validate() throws InvalidModelException {
        if (model.getNextNodes(this).size() != 1) {
            throw new InvalidModelException(this.name + ": Not single out from node");
        } else {
            next = model.getNextNodes(this).toArray(new Node[0])[0];
        }
    }

    @Override
    public JDialog getPropertiesDialog() {
        return new PrioritySetterDialog(this);
    }
}
